/**
 * 依赖包：jquery.min.js、jquery.eui.min.js、etb-collection.js
 */
Dialog = function() {
	this.open = function(jsonParam) {
		var counter = 0;//token计数器
		var _dialog = $("<div style='padding:8px' "+(jsonParam.id ? "id='" + jsonParam.id + "'" : "")+"/>");
		if(!jsonParam.width) jsonParam.width = 600;
		if(!jsonParam.height) jsonParam.height = 300;
		if(!jsonParam.url) {alert('dialog 缺少 url'); return;}
		if(!jsonParam.title) jsonParam.title = ' ';
		if(!jsonParam.buttons && jsonParam.buttons != false) {
			jsonParam.buttons = [{ 
				text: '保存', 
				iconCls: 'icon-ok', 
				handler: function() { 
					var submitform = _dialog.find(".submitform");
					if(submitform.length == 0) {
						alert("操作页面中需要有一个submitform");
					} else if(submitform.length > 1) {
						alert("操作页面中只能有一个submitform");
					} else if(submitform.form("validate")) {
						//var options = {
					        //target: '#output1',   // target element(s) to be updated with server response 
					        //beforeSubmit: showRequest,  // pre-submit callback 
					        //success: callBackFunc  // post-submit callback</font> 
					 
					        //other available options: 
					        //url:       url         // override for form's 'action' attribute 
					        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
					        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
					        //clearForm: true        // clear all form fields after successful submit 
					        //resetForm: true        // reset the form after successful submit 
					 
					        // $.ajax options can be used here too, for example: 
					        //timeout:   3000 
					    //};
						submitform.form('submit', {
							novalidate: true,
						    onSubmit: function(param){
						    	if(jsonParam.onSubmit) {
									jsonParam.onSubmit(param);
								}
						    },
						    success:function(data) {
					    		if(data == 'token') {
					    			if(counter == 0) {
					    				counter ++;
					    				$.messager.alert('提示', '正在处理，请稍后 .....', 'info');
					    			} 
					    		} else {
					    			if(jsonParam.success) {
						    			jsonParam.success(data);
						    		}
					    			_dialog.dialog('destroy');
					    		}
						    }
						});
					}
				}
			}];
		}
		_dialog.dialog({
			href : jsonParam.iframe ? '' : jsonParam.url,
			width : jsonParam.width,
			height : jsonParam.height,
			title : jsonParam.title,
			modal : true,
			onClose : function() {
				if(jsonParam.onClose) {
					jsonParam.onClose();
				}
				$(this).dialog('destroy');
			},
			buttons : jsonParam.buttons
		});
		if(jsonParam.iframe) {
			_dialog.html('<iframe src="' + jsonParam.url + '" frameborder=0 height=100% width=100% scrolling=auto></iframe>');
		}
		return new DialogUtil(_dialog);
	};
}
var dialog = new Dialog();


DialogUtil = function(arg0) {
	this._dialog = arg0;
};
DialogUtil.prototype.show = function() {
	
};
DialogUtil.prototype.destroy = function() {
	this._dialog.dialog('destroy');
}